<?php
class JournalController extends Epic_Controller_Action 
{	
	/**
	 * Authentication
	 *
	 * @see Epic_Controller_Action::init()
	 * @return void;
	 */
	public function init()
	{
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
		$action = array("index", "new", "edit", "delete");
		if (!Epic::getSession()->get('role') && in_array($this->getRequest()->getActionName(), $action)) {
			$this->_redirect(Epic::getUrl());
		}
	}
	
  	public function listAction () {
  		Epic::setTitle("List Journals");
  		
  		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
  		Epic::addListJsUrl('admin/table/jquery.dataTables.min.js');
  		Epic::addListJsUrl('admin/table/table.js');
  		Epic::addListJsUrl('admin/ajax.js');
  		
  	}
	public function addJournalAction () {
		Epic::setTitle("Add Journal");
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
		if($data = $this->getRequest()->getPost()){//bat su kien submit
			if ($data['name']=="") {
  				Epic::setError("Please enter the Name Journal");  				
			} else if ($data['subject'] == ""){
  				Epic::setError("Please enter the Subject");
			}else
			{//luu du lieu 
				Epic::getModel('admin/journal')->save($data);
				Epic::setSuccess('The journal has been saved.');
				$this->_redirect (Epic::getUrl ('admin/journal/list'));
			}
		}
	}
	public function viewJournalAction () {
		Epic::setTitle("View Journal");
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
			
		Epic::addListJsUrl('admin/table/jquery.dataTables.min.js');
  		Epic::addListJsUrl('admin/table/table.js');
  		
  	}
	public function addPagerAction () {
		Epic::setTitle("Add Pager");
		if (Epic::getSession()->get('admin')==false)
			$this->_redirect(Epic::getUrl('admin/acc/login'));
		if($data = $this->getRequest()->getPost()){//bat su kien submit
			if ($data['name']=="") {
  				Epic::setError("Please enter the Name pager");
			}
			else if ($data['doi'] == ""){
  				Epic::setError("Please enter the doi");
			}
			else if ($data['year'] == ""){
  				Epic::setError("Please enter the year");
			}
			else if ($data['page_start'] == ""){
  				Epic::setError("Please enter the pager_start");
			}
			else if ($data['page_end'] == ""){
  				Epic::setError("Please enter the pager_end");
			}else{//luu du lieu
				$pager = array (
					'id_journal' => $data['id_journal'],
					'name' => $data['name'],
					'volume' => $data['volume'],
					'issue' => $data['issue'],
					'doi' => $data['doi'],	
					'year' => $data['year'],
					'page_start' => $data['page_start'],
					'page_end' => $data['page_end'],
					'isss' => $data['isss'],
					'abstract' => $data['abstract']
				);
				
				if (isset($_POST['id_pager'])) { 
					$pager['id_pager'] = $_POST['id_pager'];
				}
				echo "<pre>";
				var_dump ($pager); 
				Epic::getModel('admin/pager')->save($pager);
				Epic::setSuccess('The pager has been saved.');
				// 	save for topic
				if (isset($data['topics'])) {
					if (isset($_POST['id_pager'])) {
						$id_pager = $_POST['id_pager'];
					} else {
						$id_pager =Epic::getModel('admin/pager')->getInsertId(); 
					}
					foreach ($data['topics'] as $id_topic) {
						$query = "INSERT INTO `topic_pager` (`id_topic`, `id_pager`) VALUES ('".$id_topic."', '".$$id_pager."')";
						Epic::getAdapter()->query($query)->fetchOne();
					}
				}
				$this->_redirect(Epic::getUrl('admin/journal/viewJournal',array('id_journal'=>$data['id_journal'])));			}
		}
  	}
}